package com.haitaoit.chengquan.main;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.bean.ShowLineBeen;
import com.haitaoit.chengquan.module.home.adapter.parts.ShowLineAdapter;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.DateTimeUtil;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.MyListView;
import com.haitaoit.chengquan.view.PullToRefreshBase;
import com.haitaoit.chengquan.view.PullToRefreshScrollView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by 赵航 on 2017/12/6.
 */

public class ShowOutLine extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ScrollView> {

    @ViewInject(R.id.pullrefreshscrollview)
    PullToRefreshScrollView pullrefreshscrollview;
    @ViewInject(R.id.left)
    ImageView               left;
    private LinearLayout    mLinearLayout;
    private MyListView      mListView;
    private ScrollView      mScrollView;
    private ShowLineAdapter mAdapter;
    private boolean isFrist = true;
    private int     page    = 1;

    List<ShowLineBeen.ResponseBean> new_list = new ArrayList<ShowLineBeen.ResponseBean>();
    private String mId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showoutline);
        ViewUtils.inject(this);


        View view = LayoutInflater.from(this).inflate(R.layout.activity_showline, null);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mLinearLayout = (LinearLayout) view.findViewById(R.id.null_information_layout);
        mListView = (MyListView) view.findViewById(R.id.scrollview_listview);
        pullrefreshscrollview.setPullLoadEnabled(true);
        pullrefreshscrollview.setPullRefreshEnabled(false);
        pullrefreshscrollview.setOnRefreshListener(this);
        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
        mScrollView = pullrefreshscrollview.getRefreshableView();
        mScrollView.setFillViewport(true);
        mScrollView.setVerticalScrollBarEnabled(false);
        mScrollView.addView(view);

        mAdapter = new ShowLineAdapter(this);
        mListView.setAdapter(mAdapter);
        //查询的id
        mId = getIntent().getStringExtra("id");

        initNetData();
    }

    private void initNetData() {

        Map<String, String> map = new HashMap<String, String>();
        map.put("user_id", mId);
        map.put("page", page+"");
        map.put("pagesize", Constant.PageSize);
        final String sign = GetSign.getSign(map);
        showDailog("");
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils
                        .get()
                        .url(Constant.RECORDLIST)
                        .addParams("user_id", mId)
                        .addParams("page", page+"")
                        .addParams("pagesize", Constant.PageSize)
                        .addParams("sign", sign)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                hindDailog();
                                ToastUtils.showToast("信息显示失败");
                                return;
                            }
                            @Override
                            public void onResponse(String response, int id) {
                                hindDailog();
                                Gson gson = new Gson();
                                ShowLineBeen been = gson.fromJson(response, ShowLineBeen.class);
                                if (been == null) {
                                    ToastUtils.showToast("信息显示失败");
                                    return;
                                }
                                if (!TextUtils.equals(been.getErrCode(), "0")) {
                                    ToastUtils.showToast(been.getErrMsg());
                                    return;
                                }
                                if (TextUtils.equals(been.getErrCode(), "0")) {
                                    List<ShowLineBeen.ResponseBean> beanList = been.getResponse();
                                    if (isFrist) {
                                        new_list.clear();
                                        if (isNullDate(beanList)) {
                                            new_list.addAll(beanList);
                                            mAdapter.setList(new_list);
                                            mAdapter.notifyDataSetChanged();
                                        }
                                        pullrefreshscrollview.onPullDownRefreshComplete();
                                    } else {
                                        List<ShowLineBeen.ResponseBean> allList = mAdapter.getList();
                                        allList.addAll(new_list);
                                        mAdapter.setList(allList);
                                        mAdapter.notifyDataSetChanged();
                                        pullrefreshscrollview.onPullUpRefreshComplete();
                                    }


                                }

                            }
                        });
            }
        }).start();

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
        isFrist = true;
        page = 1;
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
        pullrefreshscrollview.setLastUpdatedLabel(DateTimeUtil
                .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
        isFrist = false;
        page++;
    }

    /**
     * @tab 判断listview是否有值
     */
    private boolean isNullDate(List<ShowLineBeen.ResponseBean> rewordList) {
        if (rewordList == null || rewordList.size() == 0) {
            mLinearLayout.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
            return false;
        } else {
            mLinearLayout.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
            return true;
        }
    }
}
